package de.dubiel.plung.client.gin;

import com.google.gwt.core.client.GWT;
import com.google.gwt.inject.client.AbstractGinModule;
import com.google.gwt.place.shared.PlaceController;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.event.shared.SimpleEventBus;

import de.dubiel.plung.client.pages.account.AccountView;
import de.dubiel.plung.client.pages.account.AccountViewImpl;
import de.dubiel.plung.client.pages.game.GameView;
import de.dubiel.plung.client.pages.game.GameViewImpl;
import de.dubiel.plung.client.pages.start.StartView;
import de.dubiel.plung.client.pages.start.StartViewImpl;
import de.dubiel.plung.client.play.GameManager;
import de.dubiel.plung.client.services.UserService;
import de.dubiel.plung.client.services.UserServiceAsync;

public class ClientModule extends AbstractGinModule {
    private UserServiceAsync userService;
    private PlaceController placeController;

    @Override
    protected void configure() {
        bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);
        bind(GameView.class).to(GameViewImpl.class).in(Singleton.class);
        bind(AccountView.class).to(AccountViewImpl.class).in(Singleton.class);
        bind(StartView.class).to(StartViewImpl.class).in(Singleton.class);
        bind(UserProvider.class).in(Singleton.class);
        bind(GameManager.class).in(Singleton.class);
    }

    @Provides
    PlaceController getPlaceController(EventBus eventBus) {
        if (placeController == null)
            placeController = new PlaceController(eventBus);
        return placeController;
    }

    @Provides
    UserServiceAsync getUserServiceAsync() {
        if (userService == null)
            userService = GWT.create(UserService.class);
        return userService;
    }
}